import 'dart:io';

import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'package:flutter_lib/utils/LogUtils.dart';
import 'package:flutter_lib/utils/ShowDialogUtils.dart';
import 'package:flutter_my_web/utils/Utils.dart';
import 'package:flutter_my_web/widget/AppBarTitleWidget.dart';
import 'package:flutter_my_web/widget/BlueMaterialButton.dart';

class ChangeFileStrPage extends StatefulWidget {
  const ChangeFileStrPage({Key? key}) : super(key: key);

  @override
  State<ChangeFileStrPage> createState() => _ChangeFileStrPageState();
}

class _ChangeFileStrPageState extends State<ChangeFileStrPage> {
  String directoryPathStr = "";
  var rsList = [];
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: AppBarTitleWidget("替换文件字符串"),
      ),
      body: Column(
        children: [
          BlueMaterialButton("选择文件夹", () async {
            var directoryPath = await FilePicker.platform.getDirectoryPath();
            LogUtils.e("directoryPath:${directoryPath}");
            if (directoryPath == null) {
              showSnackBar("没有选择文件夹");
              return;
            }
            directoryPathStr = directoryPath;
            rsList = [];
            await actionDirectory(directoryPath);
            File file = File("C:\\Users\\hy\\Desktop\\test_replace\\1.txt");
            if(!file.existsSync()){
              await file.create();
            }
            var str = "";
            for(var i=0;i<rsList.length;i++){
              str+=rsList[i]+"\n";
            }
            file.writeAsString(str,flush: true);
            setState(() {});
            showSnackBar("完成");
          }),
        ],
      ),
    );
  }


  actionDirectory(String directoryPath) async {
    var directory = Directory(directoryPath);
    var list = await directory.list(recursive: false).toList();
    for(var i=0;i<list.length;i++){
      var element = list[i];
      if(element.statSync().type==FileSystemEntityType.directory){
        await actionDirectory(element.path);
      }else{
        print("element:${element.path}");
        File file = new File(element.path);
        String content = await file.readAsString();
        RegExp reg = new RegExp("[\"|']\\w*[\"|'].HYTr");
        content = replace1(content, reg);
        content = content.replaceAll("import 'package:cloud_mobile/translation/messages.dart';", "");
        // content = replace2(content, reg2);
        content = "import 'package:cloud_mobile/translation/message_value.dart';\n"+content;
        await file.writeAsString(content);
      }
    }
  }

  String replace1(String content,RegExp regExp){
    Iterable<Match> matches = regExp.allMatches(content);
    var listReplaceStrs = [];
    var replaceends = [];
    for (Match m in matches) {
      var replaceStr = content.substring(m.start,m.end);
      var re = replaceHYTStr1(replaceStr);
      var repend = "RString.${re}";
      listReplaceStrs.add(replaceStr);
      replaceends.add(repend);
      var s = "static var ${re} = '${re}'.HYTr;";
      if(!rsList.contains(s)){
        rsList.add(s);
      }
      print("replaceStr:$replaceStr  =>   $repend");
    }
    for(int i = 0;i<listReplaceStrs.length;i++){
      content = content.replaceAll(listReplaceStrs[i], replaceends[i]);
    }
    return content;
  }
  // "NetworkTypeEnum_NONORMAL".HYTr => NetworkTypeEnum_NONORMAL
  String replaceHYTStr1(String str){
    var ret = "";
    RegExp reg = new RegExp("[\"|']\\w*[\"|']");
    Iterable<Match> matches = reg.allMatches(str);
    ret = str.substring(matches.first.start+1,matches.first.end-1);
    return ret;
  }

}
